package com.ruoyi.dispatch.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.dispatch.domain.*;
import com.ruoyi.dispatch.enums.TunnelDayReportCol;
import com.ruoyi.dispatch.service.IDrivingTeamService;
import com.ruoyi.dispatch.service.ITunnelInfoService;
import com.ruoyi.dispatch.vo.TunnelDayReportVO;
import com.ruoyi.framework.utils.BeanCopyUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dispatch.service.ITunnelDayReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 掘进日报Controller
 *
 * @author wuxiaofang
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/dispatch/drivingDayReport")
@Anonymous
public class TunnelDayReportController extends BaseController
{
    @Autowired
    private ITunnelDayReportService tunnelDayReportService;

    @Autowired
    private IDrivingTeamService drivingTeamService;

    @Autowired
    private ITunnelInfoService tunnelInfoService;

    /**
     * 查询掘进日报列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(TunnelDayReport tunnelDayReport,String[] columnArray)
    {
        startPage();
        List<TunnelDayReport> list = tunnelDayReportService.selectTunnelDayReportList(tunnelDayReport);
        List<Map<String,String>> columns = null;
        if(columnArray!=null && columnArray.length!=0){
            columns = tunnelDayReportService.handleColumnMap(columnArray);
        }
        TableDataInfo tableData =  getDataTable(list);
        tableData.setColumns(columns);
        return tableData;
    }

    /**
     * 导出掘进日报列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:export')")
    @Log(title = "掘进日报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TunnelDayReport tunnelDayReport)
    {
        List<TunnelDayReport> list = tunnelDayReportService.selectTunnelDayReportList(tunnelDayReport);
        ExcelUtil<TunnelDayReport> util = new ExcelUtil<TunnelDayReport>(TunnelDayReport.class);
        util.exportExcel(response, list, "掘进日报数据");
    }

    /**
     * 获取掘进日报详细信息
     */
   // @PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return success(tunnelDayReportService.selectTunnelDayReportByReportId(reportId));
    }

    /**
     * 新增掘进日报
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:add')")
    @Log(title = "掘进日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TunnelDayReport tunnelDayReport)
    {
       // tunnelDayReport.setCreateBy(getUsername());
        //临时兼容处理 根据巷道/队组名称查id

        if(tunnelDayReport.getTunnelName()!=null){
            TunnelInfo tunnelInfo = new TunnelInfo();
            tunnelInfo.setTunnelName(tunnelDayReport.getTunnelName());
            List<TunnelInfo> tunnelInfos = tunnelInfoService.selectTunnelInfoList(tunnelInfo);
            if(tunnelInfos.size()>0){
                tunnelDayReport.setTunnelId(tunnelInfos.get(0).getTunnelId());
            }
        }
        if(tunnelDayReport.getTeamName()!=null){
            DrivingTeam param = new DrivingTeam();
            param.setTeamName(tunnelDayReport.getTeamName());
            List<DrivingTeam> drivingTeams = drivingTeamService.selectDrivingTeamList(param);
            if(drivingTeams.size()>0){
                tunnelDayReport.setTeamId(drivingTeams.get(0).getDrivingTeamId());
            }
        }

        int insertResult = tunnelDayReportService.insertTunnelDayReport(tunnelDayReport);
        //查询此次插入的记录
        TunnelDayReport record = tunnelDayReportService.selectTunnelDayReportByReportId(tunnelDayReport.getReportId());
        TunnelDayReportVO vo = BeanCopyUtils.copyProperties(record,TunnelDayReportVO.class);
        //获取默认展示列名
        vo.setDisplayCols(TunnelDayReportCol.getDefalutCols());
        return toAjax(insertResult,vo);
    }

    /**
     * 修改掘进日报
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:edit')")
    @Log(title = "掘进日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TunnelDayReport tunnelDayReport)
    {
        tunnelDayReport.setUpdateBy(getUsername());
        return toAjax(tunnelDayReportService.updateTunnelDayReport(tunnelDayReport));
    }

    /**
     * 删除掘进日报
     */
   // @PreAuthorize("@ss.hasPermi('dispatch:drivingDayReport:remove')")
    @Log(title = "掘进日报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(tunnelDayReportService.deleteTunnelDayReportByReportIds(reportIds));
    }
    @RequestMapping("countMonthYearTunnelDayReport")
    public List<MonthTunnelDayReport> countMonthYearTunnelDayReport(TunnelDayReport tunnelDayReport){
        List<MonthTunnelDayReport> monthTunnelDayReports = tunnelDayReportService.countMonthYearTunnelDayReport(tunnelDayReport);
        return monthTunnelDayReports;
    }

}
